
import { KeyGenerator } from '../common/keygenerator.js'

import svcfactory from './dragdrop/dragdropgraphfactory.js'
import containergraphsvc from './containergraphsvc.js';
import {CGraphTypeUtil} from '../graph/paintutil/graphtypeutil.js';
import globalConst from '../common/globalConst.js';
var global = globalConst.GlobalConst;
import textinfo from '../common/struct/basestruct/textinfo.js';
import { LGXEnums } from '../common/sysenum.js';


import { AlignItemToContainerSvc } from './dragdrop/alignItemToContainer.js';
import graphfactory from '../graph/graphfactory.js';
import { GlobalVar } from '../common/globalvar.js';

class DragDropSvc {
    constructor(manager) {
        this.dataManger = manager;
    }

    layGraphOnDragEvent(paintMap, mousePoint, dragType) {
        let result = { evt: null, graph: null };
        if (dragType == "enter") {
            //拖拽当作开始进入画布区域
            this.onDragEnter(paintMap, mousePoint, result);
        }
        else if (dragType == "over") {
            //拖拽过程中，鼠标未松开
            result.evt = this.onDragMoveOver(mousePoint);
        }
        else if (dragType == "drop") {
            // this.onDragEnter(paintMap,mousePoint,result);
            // result.evt = this.onDragMoveOver(mousePoint);
        }
        return result;
    }

    onDragDrop(mousePoint) {
        let curentTempGraph = this.dataManger.getCurentGraph();
        let mapscale = this.dataManger.getMapScale();
        let preCenterPt = curentTempGraph.getZone().getCnt();
        let tx = mousePoint.x - preCenterPt.x;
        let ty = mousePoint.y - preCenterPt.y;
        curentTempGraph.translate({ x: tx, y: ty }, mapscale, false);
        return null;
    }

    onDragMoveOver(mousePoint) {
        let curentTempGraph = this.dataManger.getCurentGraph();
        let mapscale = this.dataManger.getMapScale();
        let preCenterPt = curentTempGraph.getZone().getCnt();
        let tx = mousePoint.x - preCenterPt.x;
        let ty = mousePoint.y - preCenterPt.y;
        curentTempGraph.translate({ x: tx, y: ty }, mapscale, false);
        return null;
    }

    onDragEnter(paintMap, mosWorldPos, resultOut) {
        let evt = null;
        console.log("layGraphOnDragDrop**********************");
        this.dataManger.m_pDragTempGraphList = [];
        var list = new Array();
        this.dataManger.m_pOperStepService.record(1, list);
        let result = this.createGraphs(mosWorldPos);
        let pCurentGraph = result.graph;
        if (pCurentGraph) {
            let valid = this.assignGraphProp(pCurentGraph, mosWorldPos);
            if (!valid) {
                pCurentGraph.setUseful(0);
            }
            else {
                AlignItemToContainerSvc.layContainerItemGraphInContainer(pCurentGraph, mosWorldPos, this.dataManger);
                this.createLabel(paintMap, pCurentGraph);
                this.dataManger.m_pOperStepService.record(0, list);
                resultOut.graph = pCurentGraph;
            }
        }

        let tool = this.dataManger.getTool();
        var mainType = tool >> 16;
        if (mainType == global.BaseUnitImage) {
            evt = new mapevent.MapEvent;
            evt.eventType = "lmosup";//绘图画布动作类型
            evt.eventKind = LGXEnums.LGXEventKind.StartDrawImageOnMap;
            if (pCurentGraph) {
                evt.gid = graph.m_gid;
                evt.uuid = graph.m_rid;
            }
        }
        resultOut.evt = evt;
    }

    createGraphs(mosWorldPos) {
        var pCurentGraph = null;
        var toolKind = this.dataManger.getTool();

        let mainType = CGraphTypeUtil.getType32(toolKind);
        let leadwireList = null;
        let devSymbol = this.dataManger.getSymbol(this.dataManger.getToolSymbolUUID(), 0);
        if (devSymbol) {
            leadwireList = devSymbol.getLeadwireList();
        }

        let isContainerSymbol = false;
        if (mainType != LGXEnums.LGXGraphType.LineGraph_type) {
            let symbolDef = this.dataManger.getSymbolDef(this.dataManger.getToolSymbolUUID());
            if (symbolDef) {
                isContainerSymbol = symbolDef.moldActionType;
            }
        }

        var graphList = new Array();
        if (isContainerSymbol) {
            graphList = containergraphsvc.ContainerGraphSvc.create(this.dataManger, toolKind, mosWorldPos);
            if (graphList.length > 1) {
                pCurentGraph = graphList[0];
            }
        }
        else {
            pCurentGraph = this.dataManger.productGraph(toolKind);
            if (pCurentGraph) {
                if (devSymbol) {
                    pCurentGraph.setSymbol(devSymbol);
                    this.setLeadwire(pCurentGraph, leadwireList);
                }
                graphList.push(pCurentGraph);
            }
        }

        for (let i = 0; i < graphList.length; i++) {
            let graph = graphList[i];
            graph.setNewAdd(1);
            this.dataManger.m_topoManager.productNewRes4NewGraph(graph);
            graph.m_gid = KeyGenerator.gid();
            if (graph.m_topoRes) {
                graph.m_rid = graph.m_topoRes.id;
            }
        }
        return { graph: pCurentGraph, childs: graphList };
    }

    assignGraphProp(pGraph, mosWorldPos) {
        let valid = false;
        let svc = svcfactory.DragDropGraphSvcFactory.createSvcByEnums(pGraph);
        if (svc) {
            svc.assignProp(mosWorldPos, this.dataManger.getMapScale());
            pGraph.setUseful(1);
            pGraph.setStart(1);
            pGraph.setFinish(1);
            pGraph.calScope(1);
            valid = true;
        }
        return valid;
    }

    createLabel(paintMap, parentGraph) {
        if (parentGraph.m_graphType != LGXEnums.LGXGraphType.MoldGraph_type) {
            return null;
        }
        let devSymbol = parentGraph.getSymbol();
        if (!devSymbol) {
            return null;
        }
        if (devSymbol.getMoldActionType() > LGXEnums.moldActionType.common) {
            return null;
        }
        let symboName = devSymbol.getName();
        if (!symboName || (symboName && symboName.length == 0)) {
            return null;
        }

        let symbolDef = this.dataManger.getSymbolDef(devSymbol.getUUID());
        if (!symbolDef || (symbolDef && !symbolDef.autoLabel)) {
            return null;
        }

        let toolKind = global.BaseUnitText;
        toolKind = toolKind << 16;
        let pTextGraph = this.dataManger.productGraph(toolKind);
        if (pTextGraph) {
            pTextGraph.m_gid = KeyGenerator.gid();
            parentGraph.setLabel(pTextGraph);
        }
        else {
            return null;
        }

        let graphZone = parentGraph.getZone();
        let info = new textinfo.TextInfo();
        info.color = GlobalVar.backcolor.invert().toValue();
        info.gid = pTextGraph.m_gid;
        let labCntPt = this.calLabelCenterPt(symbolDef.dlftLabPos, graphZone, info);
        info.x = labCntPt.x;
        info.y = labCntPt.y;
        info.pos.x = info.x;
        info.pos.y = info.y;
        info.m_nUseful = 1;
        info.m_nNewAdd = 1;
        pTextGraph.setUseful(1);
        pTextGraph.setNewAdd(1);
        info.sOrigContent = symboName;
        this.dataManger.addCurentNewGraph(pTextGraph);
        pTextGraph.assignInfo(info, 1);
        pTextGraph.resetLabelPosInRefRectBottom(paintMap, graphZone);
        pTextGraph.dragCreateInitFlag = true;
        return pTextGraph;
    }

    calLabelCenterPt(dlftLabPosDecs, graphZone, txtInfo) {
        let cntPt = graphZone.getCnt();
        let labCntPt = { x: 0, y: 0 };
        switch (dlftLabPosDecs) {
            case "center":
                labCntPt.x = cntPt.x;
                labCntPt.y = cntPt.y;
                break;

            case "bottom_center":
                labCntPt.x = cntPt.x;
                labCntPt.y = graphZone.bottom - global.Label2ParentGap - txtInfo.fontsize;
                break;

            case "top_center":
                labCntPt.x = cntPt.x;
                labCntPt.y = graphZone.top + global.Label2ParentGap + txtInfo.fontsize;
                break;

            default:
                labCntPt.x = cntPt.x;
                labCntPt.y = cntPt.y;
                break;
        }

        return labCntPt;
    }

    generateTempGraph(toolKind, mosWorldPos) {
        let mainType = CGraphTypeUtil.getType32(toolKind);
        let isContainerSymbol = false;
        let leadwireList = null;
        if (mainType != LGXEnums.LGXGraphType.LineGraph_type) {
            let symbolDef = this.dataManger.getSymbolDef(toolKind);
            if (symbolDef) {
                isContainerSymbol = symbolDef.moldActionType;
            }

            let devSymbol = this.dataManger.getSymbol(toolKind, 0);
            if (devSymbol) {
                leadwireList = devSymbol.getLeadwireList();
            }
        }

        let graphList = [];
        if (isContainerSymbol) {
            graphList = containergraphsvc.ContainerGraphSvc.createTemp(this.dataManger, toolKind, mosWorldPos);
        }
        else {
            var devSymbol = this.dataManger.getSymbol(toolKind, 0);
            var factory = graphfactory.GraphFactory;
            var pNewGraph = factory.createGraphByType32(toolKind, devSymbol);
            if (pNewGraph) {
                if (devSymbol) {
                    pNewGraph.setSymbol(devSymbol);
                }
                graphList.push(pNewGraph);
                this.assignGraphProp(pNewGraph, mosWorldPos);
            }
        }

        graphList.forEach(p => {
            this.dataManger.m_pDragTempGraphList.push(p);
        });
        return;
    }


    setLeadwire(pCurentGraph, leadwireList) {
        if (!pCurentGraph || !leadwireList) {
            return;
        }
        for (let i = 0; i < leadwireList.length; i++) {
            let lineUnit = leadwireList[i];
            console.log(lineUnit);
            let lineGraph = this.dataManger.productGraph(lineUnit.type);
            if (lineGraph) {
                lineGraph.assignInfo(lineUnit);
                pCurentGraph.addChild(lineGraph);
            }
        }
    }

}

export default { DragDropSvc }